package com.gxa.learning.pc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Project;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.pc.mapper.ProjectMapper;
import com.gxa.learning.pc.service.ProjectService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ru
 */
@Service
public class ProjectServiceImpl extends BaseServiceImpl<Project> implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper = null;

    @Override
    public List<Project> getByPage(String name, Date start, Integer status, String man, Date end, Integer page, Integer size) throws BusinessException {
        if (page == null || page <= 0) {
            page = 1;
        }
        if (size == null || size <= 0) {
            size = 5;
        }
        Integer offset = (page - 1) * size;
        List<Project> byPage = projectMapper.getByPage(name, start, status, man, end, offset, size);
        if (byPage == null) {
            throw new BusinessException(ExceptionCode.WRONG_DATA);
        }
        return byPage;
    }

    @Override
    public Integer getByPageCount(String name, Date start, Integer status, String man, Date end) throws BusinessException {
        return projectMapper.getByPage(name, start, status, man, end, null, null).size();
    }

    /**
     * 获取学员参加的培训
     *
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> getUserProject(Map<String, Object> map) {
        return projectMapper.getUserProject(map);
    }


    @Override
    public PageInfo<Map<String, Object>> getProjectList(Integer page, Integer size, String projectName, String projectStatus, String projectMan) throws Exception {
        if (StringUtils.isBlank(projectName)) {
            projectName = null;
        }
        if (StringUtils.isBlank(projectStatus)) {
            projectStatus = null;
        }
        if (StringUtils.isBlank(projectMan)) {
            projectMan = null;
        }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> projectList = projectMapper.getProjectList(projectName, projectStatus, projectMan);

        return PageInfo.of(projectList);

    }
}
